package com.qf.micrograss.grass.task;

import com.qf.common.core.entity.Grass;
import com.qf.common.redis.annotation.MyLock;
import com.qf.common.xxljob.utils.XxlJobUtils;
import com.qf.micrograss.grass.service.IGrassService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2025/11/12
 * @desc
 */
@Component
public class GrassTask {

    @Autowired
    private IGrassService grassService;

    @Autowired
    private XxlJobUtils xxlJobUtils;

    /**
     * 定时更新点赞的任务调度
     */
    // @Scheduled(cron = "*/10 * * * * ?")// 模拟10s种一次
    @MyLock(lockKey = "locker")
    public void updateLikeNum(){
        grassService.updateLikeNumTask();
    }

    /**
     * 使用xxljob实现定时更新点赞的任务调度
     */
    @XxlJob("updateLikeNumXxlJob")
    public void updateLikeNumXxlJob(){
        grassService.updateLikeNumTask();
    }

    /**
     * 定时更新笔记状态(发布笔记)
     */
    @XxlJob("updateGrassEnableHandler")// 这个任务名字是 GrassServiceImpl类发布笔记时指定的
    public void updateGrassStatus(){
        System.out.println(">>>>>>>>> 开始更新笔记发布状态 >>>>>>>>>");
        // 获得任务参数
        String gid = XxlJobHelper.getJobParam();
        Grass grass = new Grass();
        grass.setId(Integer.parseInt(gid));
        grass.setStatus(1);
        grassService.updateById(grass);
    }
}
